package com.cslg.ac.demo.controller.message;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cslg.ac.demo.entity.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cslg.ac.demo.constant.MessageType;
import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.entity.message.Message;
import com.cslg.ac.demo.service.message.MessageService;
import com.cslg.ac.demo.util.communicate.ResponseUtil;

import javax.servlet.http.HttpSession;

@Controller
public class MessageController {

	@Autowired
	private MessageService messageService;
	
	@RequestMapping(value="/message/get")
	public void get( @RequestParam int type, PrintWriter out, HttpSession session)
	{
		User user = (User) session.getAttribute("user");
		int id = user.getId();
		if(!MessageType.isMessageType(type))
		{
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		List<Message> messages;
		try
		{
			messages= messageService.getMessage(id, type);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("message", messages);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
}
